
vehicleFuelUsage = {
    -- ZWYKŁE --
    ["1.0"] = 0.005,
    ["1.1"] = 0.005,
    ["1.2"] = 0.005,
    ["1.3"] = 0.005,
    ["1.4"] = 0.005,
    ["1.5"] = 0.005,
    ["1.6"] = 0.005,
    ["1.7"] = 0.005,
    ["1.8"] = 0.005,
    ["1.9"] = 0.005,
    ["2.0"] = 0.005,
    ["2.1"] = 0.005,
    ["2.2"] = 0.005,
    ["2.3"] = 0.005,
    ["2.4"] = 0.005,
    ["2.5"] = 0.005,
    ["2.6"] = 0.005,
    ["2.7"] = 0.005,
    ["2.8"] = 0.005,
    ["2.9"] = 0.005,
    ["3.0"] = 0.005,
    ["3.1"] = 0.005,
    ["3.2"] = 0.005,
    ["3.3"] = 0.005,
    ["3.4"] = 0.005,
    ["3.5"] = 0.005,
    ["3.6"] = 0.005,
    ["3.7"] = 0.005,
    ["3.8"] = 0.005,
    ["3.9"] = 0.005,
    ["4.0"] = 0.005,
    ["4.1"] = 0.005,
    ["4.2"] = 0.005,
    ["4.3"] = 0.005,
    ["4.4"] = 0.005,
    ["4.5"] = 0.005,
    ["4.6"] = 0.005,
    ["4.7"] = 0.005,
    ["4.8"] = 0.005,
    ["4.9"] = 0.005,
    ["5.0"] = 0.005,
    -- TURBO --
    ["1.0T"] = 0.005,
    ["1.1T"] = 0.005,
    ["1.2T"] = 0.005,
    ["1.3T"] = 0.005,
    ["1.4T"] = 0.005,
    ["1.5T"] = 0.005,
    ["1.6T"] = 0.005,
    ["1.7T"] = 0.005,
    ["1.8T"] = 0.005,
    ["1.9T"] = 0.005,
    ["2.0T"] = 0.005,
    ["2.1T"] = 0.005,
    ["2.2T"] = 0.005,
    ["2.3T"] = 0.005,
    ["2.4T"] = 0.005,
    ["2.5T"] = 0.005,
    ["2.6T"] = 0.005,
    ["2.7T"] = 0.005,
    ["2.8T"] = 0.005,
    ["2.9T"] = 0.005,
    ["3.0T"] = 0.005,
    ["3.1T"] = 0.005,
    ["3.2T"] = 0.005,
    ["3.3T"] = 0.005,
    ["3.4T"] = 0.005,
    ["3.5T"] = 0.005,
    ["3.6T"] = 0.005,
    ["3.7T"] = 0.005,
    ["3.8T"] = 0.005,
    ["3.9T"] = 0.005,
    ["4.0T"] = 0.005,
    ["4.1T"] = 0.005,
    ["4.2T"] = 0.005,
    ["4.3T"] = 0.005,
    ["4.4T"] = 0.005,
    ["4.5T"] = 0.005,
    ["4.6T"] = 0.005,
    ["4.7T"] = 0.005,
    ["4.8T"] = 0.005,
    ["4.9T"] = 0.005,
    ["5.0T"] = 0.005,
}


function action(vehicle, data)
    if not vehicle or not data then return end 
    if math.random(1, 15) <= 2 and getElementHealth(vehicle) < 400 and getVehicleEngineState(vehicle) == true then 
        exports['cmta-notyfikacje']:sendNotification("#ccc000Informacja\n#ffffffTwój pojazd jest mocno uszkodzony!\nSilnik zgasł...","info",3000)
        triggerServerEvent('cmta_spalanie:interactVehicle', getResourceRootElement(), 'engine', false)
        return
    end
end

function fuel(vehicle, data)
    if not vehicle or not data then return end 
    if data and data.fuel and tonumber(data.fuel) <= 0 and getVehicleEngineState(vehicle) then 
        triggerServerEvent('cmta_spalanie:interactVehicle', getResourceRootElement(), 'engine', false)
        return
    end
end

function takeMileage(vehicle, data)
    if not vehicle or not data then return end 
    local x,y,z = getElementPosition(vehicle)
    if not lastposition then lastposition = {} lastposition[1] = {x, y, z} end
    if getDistanceBetweenPoints3D(x, y, z, lastposition[#lastposition][1], lastposition[#lastposition][2], lastposition[#lastposition][3]) >= 5 then 
        local x,y,z = getElementPosition(vehicle)
        lastposition[#lastposition+1] = {x, y, z}
        data.mileage = data.mileage+0.2
    end
end

function render()
    if not getPedOccupiedVehicle(localPlayer) or not vehicle or vehicle ~= getPedOccupiedVehicle(localPlayer) or not isElement(vehicle) or not getElementData(vehicle, 'vehicle:data') then 
        stopCombustion()
        return 
    end 
    local data = getElementData(vehicle, 'vehicle:data')
    fuel(vehicle, data)
    if getKeyState('w') and getVehicleEngineState(vehicle) then 
        takeMileage(vehicle, data)
        if not last or last <= getTickCount()-1000 and getElementSpeed(vehicle, 'km/h') >= 2 then 
            action(vehicle, data)
        end
        if not last or last <= getTickCount()-300 then 
            if isElementMoving(vehicle) then 
                last = getTickCount()
                if getElementSpeed(vehicle, 'km/h') >= 100 then 
                    data.fuel = data.fuel-(vehicleFuelUsage[data.engine])
                else
                    data.fuel = data.fuel-vehicleFuelUsage[data.engine]
                end
                setElementData(vehicle, 'vehicle:data', data)
            end
        end
    end
end

function startCombustion(playerVehicle)
    if not playerVehicle then return end 
    local data = getElementData(playerVehicle, 'vehicle:data')
    if not data then return end 
    vehicle = playerVehicle 
    addEventHandler('onClientRender', root, render)
end

function stopCombustion()
    vehicle = nil
    removeEventHandler('onClientRender', root, render)
end

addEventHandler('onClientVehicleEnter', root, function(ped, seat)
    if (ped == localPlayer and seat == 0) then 
        startCombustion(source)
    end
end)

addEventHandler('onClientVehicleExit', root, function(ped, seat)
    if (ped == localPlayer and seat == 0) then 
        stopCombustion()
    end
end)
    

if getPedOccupiedVehicle(localPlayer) then 
    startCombustion(getPedOccupiedVehicle(localPlayer))
end



--[[
    ASSETS
]]--


function getElementSpeed(theElement, unit)
    -- Check arguments for errors
    assert(isElement(theElement), "Bad argument 1 @ getElementSpeed (element expected, got " .. type(theElement) .. ")")
    local elementType = getElementType(theElement)
    assert(elementType == "player" or elementType == "ped" or elementType == "object" or elementType == "vehicle" or elementType == "projectile", "Invalid element type @ getElementSpeed (player/ped/object/vehicle/projectile expected, got " .. elementType .. ")")
    assert((unit == nil or type(unit) == "string" or type(unit) == "number") and (unit == nil or (tonumber(unit) and (tonumber(unit) == 0 or tonumber(unit) == 1 or tonumber(unit) == 2)) or unit == "m/s" or unit == "km/h" or unit == "mph"), "Bad argument 2 @ getElementSpeed (invalid speed unit)")
    -- Default to m/s if no unit specified and 'ignore' argument type if the string contains a number
    unit = unit == nil and 0 or ((not tonumber(unit)) and unit or tonumber(unit))
    -- Setup our multiplier to convert the velocity to the specified unit
    local mult = (unit == 0 or unit == "m/s") and 50 or ((unit == 1 or unit == "km/h") and 180 or 111.84681456)
    -- Return the speed by calculating the length of the velocity vector, after converting the velocity to the specified unit
    return (Vector3(getElementVelocity(theElement)) * mult).length
end

function isElementMoving (theElement )
    if isElement ( theElement ) then                                   -- First check if the given argument is an element
       return Vector3( getElementVelocity( theElement ) ).length ~= 0
    end
    return false
 end